package cn.felord.app.security;


import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.Assert;

/**
 * The type Jwt config.
 *
 * @author Dax
 * @since 23 :32  2018/9/21
 */
@Configuration
@EnableConfigurationProperties(JwtCertificateProperties.class)
public class JwtConfig {

    /**
     * Jwt token generator jwt token generator.
     *
     * @param jwtCertificateProperties the jwt certificate properties
     * @return the jwt token generator
     */
    @Bean
    public JwtTokenGenerator jwtTokenGenerator(JwtCertificateProperties jwtCertificateProperties) {
        Assert.notNull(jwtCertificateProperties, "jwt certificate config  is null");
        return new JwtTokenGenerator(new KeyPairFactory(), jwtCertificateProperties);
    }

    /**
     * Jwt token storage jwt token storage.
     *
     * @return the jwt token storage
     */
    @Bean
    public JwtTokenStorage jwtTokenStorage() {
        return new JwtTokenCacheStorage();
    }


    /**
     * Jwt对构造器.
     *
     * @param jwtCertificateProperties the jwt certificate properties
     * @return the jwt entity builder
     */
    @Bean
    public JwtPairBuilder jwtEntityBuilder(JwtCertificateProperties jwtCertificateProperties) {
        return new JwtPairBuilder(jwtCertificateProperties.getAccessTokenExpDays(), jwtCertificateProperties.getRefreshTokenExpDays());
    }
}
